package com.promis.widget.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.promis.util.CollectionUtils;
import com.promis.util.ColorUtils;
import com.promis.util.ScreenUtils;
import com.promis.widget.R;
import com.promis.widget.recycler.LinearSpacingItemDecoration;
import com.promis.widget.recycler.RecyclerLayoutManagerHelper;

import java.util.List;

public abstract class DefaultListEditTextDialog extends Builder {

	private TextView titleView;
	private EditText editText;
	private RecyclerView recyclerView;
	private DefaultMsgListAdapter adptDefaultMsg;
	
	private AlertDialog dialog;
	
	private List<String> defaultMsgList;
	private Activity activity;

	public DefaultListEditTextDialog(Activity activity, final String[] defaultMsgList) {
		this(activity, R.style.Theme_AlertDialog, CollectionUtils.toList(defaultMsgList));
	}
	
	public DefaultListEditTextDialog(Activity activity, final List<String> defaultMsgList) {
		this(activity, R.style.Theme_AlertDialog, defaultMsgList);
	}

	public DefaultListEditTextDialog(Activity activity, int theme, final List<String> defaultMsgList) {
		super(activity, theme);
		this.defaultMsgList = defaultMsgList;
		this.activity = activity;
		
		setPositiveButton("确定", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				DefaultListEditTextDialog.this.onConfirmClick(dialog, which);
			}
		});
		setNegativeButton("取消", null);
		
		ViewGroup.LayoutParams wrapHeightParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		int titleHeight = ScreenUtils.dp2px(getContext(), 60);
		
		LinearLayout titleLayout = new LinearLayout(activity);
		titleLayout.setLayoutParams(wrapHeightParams);
		titleView = new TextView(activity);
		LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, titleHeight);
		titleParams.setMargins(30, 0, 30, 0);
		titleView.setLayoutParams(titleParams);
		titleView.setPadding(30, 30, 30, 30);
		titleView.setBackgroundResource(R.drawable.layer_stroke_gray_bottom);
		titleView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
		titleLayout.addView(titleView);
		setCustomTitle(titleLayout);
		
		LinearLayout contentLayout = new LinearLayout(activity);
		contentLayout.setLayoutParams(wrapHeightParams);
		contentLayout.setOrientation(LinearLayout.VERTICAL);
		contentLayout.setPadding(0, 0, 0, 30);
		
		TextView txtvLsv = new TextView(activity);
		txtvLsv.setText("默认信息");
		txtvLsv.setGravity(Gravity.CENTER);
		txtvLsv.setLayoutParams(wrapHeightParams);
		txtvLsv.setPadding(0, 30, 0, 30);
		contentLayout.addView(txtvLsv);

		recyclerView = new RecyclerView(activity);
		LinearLayout.LayoutParams lsvParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
		lsvParams.setMargins(30, 0, 30, 0);
		recyclerView.setLayoutParams(lsvParams);
		RecyclerLayoutManagerHelper.buildVerticalLinearLayoutManager(activity, recyclerView);
		recyclerView.addItemDecoration(new LinearSpacingItemDecoration(activity, LinearLayoutManager.VERTICAL, 20, R.color.bg_transparent));
		adptDefaultMsg = new DefaultMsgListAdapter(activity, defaultMsgList);
		recyclerView.setAdapter(adptDefaultMsg);
		adptDefaultMsg.setOnItemClickListener(new DefaultMsgListAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(View view, String msg) {
				DefaultListEditTextDialog.this.onListItemClick(msg);
				dialog.dismiss();
			}
		});
		contentLayout.addView(recyclerView);

		TextView txtvEdit = new TextView(activity);
		txtvEdit.setText("自定义信息");
		txtvEdit.setGravity(Gravity.CENTER);
		txtvEdit.setLayoutParams(wrapHeightParams);
		txtvEdit.setPadding(0, 30, 0, 30);
		contentLayout.addView(txtvEdit);
		
		editText = new EditText(activity);
		LinearLayout.LayoutParams lineParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ScreenUtils.dp2px(getContext(), 50));
		lineParams.setMargins(30, 0, 30, 0);
		editText.setLayoutParams(lineParams);
		editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
		editText.setTextColor(ColorUtils.getColor(activity, R.color.text_black_deep));
		editText.setHintTextColor(ColorUtils.getColor(activity, R.color.text_black_shallow));
		editText.setBackgroundResource(R.drawable.shape_stroke_gray_circular3);
		contentLayout.addView(editText);
		
		setView(contentLayout);
	}

	@Override
	public Builder setTitle(CharSequence title) {
		// TODO Auto-generated method stub
		titleView.setText(title);
		return super.setTitle(title);
	}
	
	@Override
	public Builder setTitle(int titleResId) {
		// TODO Auto-generated method stub
		titleView.setText(titleResId);
		return super.setTitle(titleResId);
	}
	
	@Override
	public AlertDialog show() {
		// TODO Auto-generated method stub
		dialog = super.show();
		return dialog;
	}
	
	public EditText getEditText() {
		return editText;
	}
	
	public TextView getTitleView() {
		return titleView;
	}

	public abstract void onConfirmClick(DialogInterface dialog, int which);
	
	public abstract void onListItemClick(String msg);
}
